<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--
 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
 * Copyright (C) 2003-2007 Frederico Caldeira Knabben
 *
 * == BEGIN LICENSE ==
 *
 * Licensed under the terms of any of the following licenses at your
 * choice:
 *
 *  - GNU General Public License Version 2 or later (the "GPL")
 *    http://www.gnu.org/licenses/gpl.html
 *
 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 *    http://www.gnu.org/licenses/lgpl.html
 *
 *  - Mozilla Public License Version 1.1 or later (the "MPL")
 *    http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * == END LICENSE ==
 *
 * Category dialog window.
-->
<html>
<head>
<title>Categories</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="robots" content="noindex, nofollow" />
<script type="text/javascript">
var oEditor = window.parent.InnerDialogLoaded();
var FCK = oEditor.FCK;
var FCKLang = oEditor.FCKLang;
var FCKConfig = oEditor.FCKConfig;
var FCKRegexLib = oEditor.FCKRegexLib;
var FCKTools = oEditor.FCKTools;
var FCKBrowserInfo = oEditor.FCKBrowserInfo;
var EditorDocument = oEditor.FCK.EditorDocument;

document.write( '<script src="' + FCKConfig.BasePath + 'dialog/common/fck_dialog_common.js" type="text/javascript"><\/script>' );

window.onload = function()
{
	// Translate the dialog box texts.
	oEditor.FCKLanguageManager.TranslatePage( document );

	// Load the selected link information (if any).
	InitSelected();
	SetSearchMessage( FCKLang.wikiLoadingCategories || 'loading categories...' );
	oEditor.window.parent.sajax_request_type = 'GET';
	oEditor.window.parent.sajax_do_call( 'wfSajaxSearchCategoryFCKeditor', [], InitCategoryTree );

	// Activate the "OK" button.
	window.parent.SetOkButton( true );
	window.parent.SetAutoSize( true );
};

var selectedCats;

function InitSelected()
{
	selectedCats = new Array();
	var node = EditorDocument;
	while ( node )
	{
		if ( node.nodeType == 1 && node.tagName.toLowerCase() == 'a' )
		{
			// Get the actual Link href.
			var sHRef = node.getAttribute( '_fcksavedurl' );
			if ( sHRef == null )
				sHRef = node.getAttribute( 'href', 2 ) || '';
			if ( sHRef.StartsWith( 'Category:' ) )
			{
				var select = GetE( 'xCategories' );
				var cat = sHRef.slice( 9 );
				SelectCategory( cat, -1 );
			}
		}
		node = FCKTools.GetNextNode( node, EditorDocument );
	}
}

function SelectCategory( cat, catTreeRow )
{
	var select, row = parseInt( catTreeRow );
	if ( row >= 0 )
	{
		select = GetE( 'xWikiResults' );
		cat = select.options[ row ].text;
		var lvl = 0;
		while ( cat.charAt( lvl ) == placeholder )
			lvl++;
		cat = cat.slice( lvl );
		if ( cat.charAt( 0 ) == '[' && cat.charAt( cat.length - 1 ) == ']' )
			cat = cat.substring( 1, cat.length - 1 );
	}

	if ( selectedCats[ cat ] )
		delete selectedCats[ cat ];
	else
		selectedCats[ cat ] = cat;

	select = GetE( 'xCategories' );

	while ( select.options.length > 0 )
		select.remove( 0 );

	for ( cat in selectedCats )
		FCKTools.AddSelectOption( select, cat, cat );
}

var catTree;

function InitCategoryTree( result )
{
	SetSearchMessage( FCKLang.wikiLnkStartTyping || 'start typing in the above field' );

	catTree = new Object();
	var levelsHead = new Array( 'root' );
	var levelsBody = new Array( '' );

	var results = result.responseText.Trim().split( '\n' );
	var previousLvl = -1;
	for ( var i = 0 ; i < results.length ; i++ )
	{
		var lvl = 0;
		while ( results[ i ].charAt( lvl ) == ' ' )
			lvl++;
		var t = results[ i ].slice( lvl );
		for ( var j = previousLvl ; j > lvl - 1 ; j-- )
		{

			if ( levelsBody[ j + 1 ] != '' )
				catTree[ levelsHead[ j + 1 ] ] = levelsBody[ j + 1 ];
			delete levelsHead[ j + 1 ];
			delete levelsBody[ j + 1 ];
		}
		if ( lvl > previousLvl )
			levelsBody[ lvl ] = t;
		else
			levelsBody[ lvl ] = levelsBody[ lvl ] + ' ' + t;
		levelsHead[ lvl + 1 ] = t;
		levelsBody[ lvl + 1 ] = '';
		previousLvl = lvl;
	}
	for ( var j = previousLvl ; j >= -1 ; j-- )
	{
		if ( levelsBody[ j + 1 ] != '' )
			catTree[ levelsHead[ j + 1 ] ] = levelsBody[ j + 1 ];
		delete levelsHead[ j + 1 ];
		delete levelsBody[ j + 1 ];
	}

	ShowCategoriesSubTree( -1 );
}

var placeholder = '.';

//draw category subtree
function ShowCategoriesSubTree( rowInTree )
{
	var row = parseInt( rowInTree );
	var select = GetE( 'xWikiResults' );
	var root = 'root';
	var lvl = -1;
	var prefix = '';
	if ( row >= 0 )
	{
		root = select.options[ row ].text;
		lvl = 0;
		while ( root.charAt( lvl ) == placeholder )
			lvl++;
		root = root.slice( lvl );
		if ( root.charAt( 0 ) == '[' && root.charAt( root.length - 1 ) == ']' )
			root = root.substring( 1, root.length - 1 );
		prefix = new Array( lvl + 1 + 3 ).join( placeholder );
	}
	if ( !catTree[ root ] )
		return;

	var itCount = select.options.length;
	var itSkip = row + 1;
	var opts = new Array();
	for ( var i = row + 1 ; i < itCount ; i++ )
	{
		var t = select.options[ i ].text;
		var sublvl = 0;
		while ( t.charAt( sublvl ) == placeholder )
			sublvl++;
		if ( sublvl > lvl )
			itSkip = i + 1;
		else
			break;
	}
	for ( var i = itCount - 1 ; i > row ; i-- )
	{
		var t = select.options[ i ].text;
		if ( i >= itSkip )
			opts.push( t );
		select.remove( i );
	}
	if ( itSkip == row + 1 )
	{
		var cats = catTree[ root ].split( ' ' );

		for ( var k in cats )
		{
			var p = cats[ k ];
			if ( catTree[ cats[ k ] ] )
				p = '[' + p + ']';
			var e = FCKTools.AddSelectOption( select, prefix + p, ++row );
			if ( catTree[ cats[ k ] ] )
				e.style.color = '#00f';

		}
	}
	for ( var i = opts.length - 1 ; i >= 0 ; i-- )
	{
		var e = FCKTools.AddSelectOption( select, opts[ i ], ++row );
		if ( opts[ i ].indexOf( '[' ) >= 0 )
			e.style.color = '#00f';
	}

}

//draw filtered
function ShowFilteredCategories( filter )
{
	var select = GetE( 'xWikiResults' );
	while ( select.options.length > 0 )
		select.remove( 0 );
	var found = new Object();
	if ( filter.length == 0 )
	{
		ShowCategoriesSubTree( -1 );
		return;
	}
	filter = filter.toLowerCase();
	var row = -1;
	for ( var folder in catTree )
	{
		var cats = catTree[ folder ].split( ' ' );
		for ( var k in cats )
		{
			var p = cats[ k ].toLowerCase();
			if ( p.indexOf( filter ) >= 0 )
			{
				if ( found[ cats[ k ] ] )
					;
				else
				{
					found[ cats[ k ] ] = cats[ k ];
					FCKTools.AddSelectOption( select, cats[ k ], ++row );
				}
			}
		}
	}
}

function AddNew()
{
	var select = GetE( 'txtUrl' );
	if ( select.value.Trim() )
		SelectCategory( select.value, -1 );
	select.value = '';
}

//#### The OK button was hit.
function Ok()
{
	var nodes = new Array();
	var node = EditorDocument;
	var nodeNext;
	var s = '';
	var i = 0;
	while ( node )
	{
		nodeNext = FCKTools.GetNextNode( node, EditorDocument );
		if ( node.nodeType == 1 && node.tagName.toLowerCase() == 'a' )
		{
			// Get the actual Link href.
			var sHRef = node.getAttribute( '_fcksavedurl' );
			if ( sHRef == null )
				sHRef = node.getAttribute( 'href', 2 ) || '';
			if ( sHRef.StartsWith( 'Category:' ) )
				nodes[ i++ ] = node;
		}
		node = nodeNext;
	}
	for ( var i = 0 ; i < nodes.length ; i++ )
		nodes[ i ].parentNode.removeChild( nodes[ i ] );

	for ( var cat in selectedCats )
		AddCategoryLink( cat );

	CleanUpCategoryLinks();

	return true;
}

function CleanUpCategoryLinks()
{
	var node = EditorDocument;
	var nodes = [];
	while ( node )
	{
		if ( node.nodeType == 1 && node.tagName.toLowerCase() == 'a' )
		{
			// Get the actual Link href.
			var sHRef = node.getAttribute( '_fcksavedurl' );
			if ( sHRef == null )
				sHRef = node.getAttribute( 'href', 2 ) || '';
			if ( sHRef.StartsWith( 'Category:' ) )
				nodes.push(node);
		}
		node = FCKTools.GetNextNode( node, EditorDocument );
	}

	for ( var i = 0; i < nodes.length ; i++ )
		EditorDocument.body.appendChild( nodes[i] );
}

function AddCategoryLink( cat )
{
	var sUri = 'Category:' + cat;
	var sInnerHtml;

	// If no link is selected, create a new one (it may result in more than one link creation - #220).
	var aLinks = oEditor.FCK.CreateLink( sUri );

	// If no selection, no links are created, so use the uri as the link text (by dom, 2006-05-26)
	var aHasSelection = (aLinks.length > 0);
	if ( !aHasSelection )
	{
		sInnerHtml = sUri;

		var oLinkPathRegEx = new RegExp( "//?([^?\"']+)([?].*)?$" );
		var asLinkPath = oLinkPathRegEx.exec( sUri );
		if ( asLinkPath != null )
			sInnerHtml = asLinkPath[ 1 ]; // use matched path

		// Create a new (empty) anchor.
		aLinks = [ oEditor.FCK.InsertElement( 'a' ) ];
	}

	oEditor.FCKUndo.SaveUndoStep();

	for ( var i = 0 ; i < aLinks.length ; i++ )
	{
		oLink = aLinks[ i ];

		if ( aHasSelection )
			sInnerHtml = oLink.innerHTML; // Save the innerHTML (IE changes it if it is like an URL).

		oLink.href = sUri;
		SetAttribute( oLink, '_fcksavedurl', sUri );

		oLink.innerHTML = sInnerHtml; // Set (or restore) the innerHTML
	}

	return true;
}

//#### Called while the user types the URL.
function OnUrlChange()
{
	var link = GetE( 'txtUrl' ).value.Trim();
	ShowFilteredCategories( link );
	return;
}

function SetSearchMessage( message )
{
	GetE( 'xWikiSearchStatus' ).innerHTML = message;
}
</script>
</head>
<body scroll="no" style="overflow: hidden">
<div id="divInfo">
<div id="divLinkTypeUrl"><span fcklang="wikiSelectedCategories">Selected categories</span><br />
<select id="xCategories" size="10" style="width: 100%; height: 70px"
	ondblclick="SelectCategory( this.value,-1);">
</select><br />
<span fcklang="wikiSearchCategory">Search category</span><br />
<input id="txtUrl" style="width: 76%" type="text"
	onkeyup="OnUrlChange();" /> <input id="btnNew" style="width: 22%"
	type="button" onclick="AddNew();" value="Add new" fcklang="wikiAddNewCategory"/> <br />
<span fcklang="wikiCategoryTree">Category tree</span> (<span fcklang="wikiLnkStartTyping" id="xWikiSearchStatus">start typing in the
above field</span>)<br />
<select id="xWikiResults" size="10" style="width: 100%; height: 300px"
	onclick="ShowCategoriesSubTree( this.value );"
	ondblclick="SelectCategory('', this.value );">
</select></div>
</div>
</body>
</html>
